/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import com.myapp.struts.UsuarioHelper;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author ramiro
 */
public class Gpla extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

   
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String gpla = request.getParameter("gpla");
        
        if (gpla.equals("consultar") || gpla.equals("gestionPla") ||
                gpla.equals("agregar1")){
            return mapping.findForward(gpla);            
        }
        
        if (gpla.equals("agregar")){
            UsuarioHelper helper = new UsuarioHelper();
            
            String nombre = request.getParameter("nombre");
            List<String> parametros = new LinkedList<String>(); 
            String[] atributos = request.getParameter("atributos").split(";");
            parametros.addAll(Arrays.asList(atributos));
            helper.crearTabla(nombre,parametros);
            return mapping.findForward("agregar");
            
        }
        else if (gpla.equals("eliminar")){
            UsuarioHelper helper = new UsuarioHelper();
            String nombre = request.getParameter("nombre");
            List<String> parametros = new LinkedList<String>(); 
            String[] atributos = request.getParameter("atributos").split(";");
            parametros.addAll(Arrays.asList(atributos));
            helper.crearTabla(nombre,parametros);
            return mapping.findForward("eliminar");
        }
        else if (gpla.equals("eliminarA")){
            UsuarioHelper helper = new UsuarioHelper();    
            String nombre = request.getParameter("nombre");
            String[] atributos = request.getParameter("atributos").split(";");          
            for (int i =0;i<atributos.length;i++){   
                helper = new UsuarioHelper();
                helper.eliminarAtributo(nombre,atributos[i]);
            }
            return mapping.findForward("eliminarA");
        }
        else if (gpla.equals("agregarA")){
            UsuarioHelper helper = new UsuarioHelper();    
            String nombre = request.getParameter("nombre");
            String[] atributos = request.getParameter("atributos").split(";");
            for (int i =0;i<atributos.length;i++){
                helper = new UsuarioHelper();
                helper.agregarAtributo(nombre,atributos[i]);
            }
            return mapping.findForward("agregarA");
        }
        
        return mapping.findForward(SUCCESS);
    }
}